#!/bin/sh

# USB Network Gate daemon SysV init script
# Copyright (C) 2000-2021 H3C Team, Inc.

# This file should be copied to /etc/init.d (Debian) or /etc/rc.d/init.d (Fedora)

# chkconfig: 2345 80 20
# description: USB Network Gate daemon

### BEGIN INIT INFO
# Provides: camrdr
# Required-Start: $syslog $local_fs $network
# Required-Stop: $syslog $local_fs $network
# Should-Start: $time $named
# Should-Stop: $time $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Camera redirect daemon
# Description: Camera redirect daemon by H3C Team, Inc.
### END INIT INFO

# See: /etc/init.d/skeleton or /usr/share/doc/initscripts-*/sysvinitfiles

NAME=cameraservice
DAEMON=/opt/camrdr/$NAME

test -x "$DAEMON" || exit 0 # exit if the package is removed but not purged

if which initctl >/dev/null 2>&1 && initctl version | grep -q upstart && test -x /lib/init/upstart-job
then
	. /lib/init/upstart-job "$(basename "$0")" "$@"
	exit
fi
     
if which start-stop-daemon >/dev/null 2>&1 ; then
	DEB=1
#	. /lib/lsb/init-functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
	RPM=1
	. /etc/rc.d/init.d/functions
fi

case "$1" in
	start)
		#precheck
		/opt/camrdr/camrdr_precheck.sh
		/bin/bash -c 'cd $(InstallPath);source cam_env;./cameraservice -e >/dev/null'
		;;
	stop)
		P_ID=`ps gaux | grep ${NAME} | grep -v grep | awk '{print $2}'`
		if [ -n "$P_ID" ]
		then
			/opt/camrdr/camrdr_stop.sh $P_ID
		else
			echo "${NAME} is NOT running!!"
		fi
		;;
	force-stop)
		killall -KILL $NAME
		;;	
	restart|condrestart|try-restart|force-reload)
		"$0" stop
		"$0" start
		;;
	force-restart)
		"$0" force-stop
		"$0" start
		;;
	reload)
		P_ID=`ps gaux | grep ${NAME} | grep -v grep | awk '{print $2}'`
		if [ -n "$P_ID" ]
		then
			/bin/kill -HUP $P_ID
		else
			echo "${NAME} is NOT running!!"
		fi
		;;
	status)
		if [ "$(pidof $DAEMON)" ] ; then # status "$(basename "$0")" always false on RHEL5
			echo "$NAME is running"
		else
			echo "$NAME is not running"
		fi 
		;;
	*)
		echo "Usage: $(basename "$0") {start|stop|force-stop|restart|condrestart|try-restart|force-restart|reload|force-reload|status}"
		exit 3
		;;
esac
